// let students = [1, 1, 1, 0, 0, 1];
// let sandwiches = [1, 0, 0, 0, 1, 1];

// var countStudents = function (students, sandwiches) {
//     while (students.length > 0) {
//         if (students[0] === sandwiches[0]) {
//             // 如果队首学生和栈顶三明治匹配，移除学生和三明治
//             students.shift();
//             sandwiches.shift();
//         } else {
//             // 如果不匹配，将队首学生移到队尾
//             let student = students.shift();
//             students.push(student);
//         }
//         // 检查是否进入死循环（即所有学生都不想要栈顶的三明治）
//         if (!students.some(student => student === sandwiches[0])) {
//             break;
//         }
//     }
//     // 返回剩余学生的数量
//     return students.length;
// };

// console.log(countStudents(students, sandwiches));

let students = [1, 1, 1, 0, 0, 1];
let sandwiches = [1, 0, 0, 0, 1, 1];
var countStudents = function (students, sandwiches) {
    let s1 = students.filter(student => student === 1).length;
    let s0 = students.length - s1;
    for (let i = 0; i < sandwiches.length; i++) {
        if (sandwiches[i] === 0 && s0 > 0) {
            s0--;
        } else if (sandwiches[i] === 1 && s1 > 0) {
            s1--;
        } else {
            break;
        }
    }
    return s0 + s1;
};
console.log(countStudents(students, sandwiches));
